﻿using BaoVeBienCuong.Playgame.enemy;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;

namespace BaoVeBienCuong.Playgame.army
{
    class Dan
    {
        bool isEnable;
        int loai;
        float tocdo;
        Vector2 duongdi;
        Vector2 diemdau;
        Vector2 diemdich;
        Texture2D image;
        Vector2 size;
        float scale;
        float satthuong;
        Vector2 position;
        Vector2 dolech;
        float gocquay;
        string hieuung = "";
        int target;
        public void setEnable(bool tt)
        {
            isEnable = tt;
        }
        public bool getEnable()
        {
            return isEnable;
        }
        public Dan(int loai, float tocdo, Vector2 diemdau, float satthuong)
        {
            target = 0;
            isEnable = false;
            this.satthuong = satthuong;
            this.loai = loai;
            this.tocdo = tocdo;
            this.diemdau = diemdau;
            position = diemdau;
            scale = 0.4f;
            size = new Vector2(768 / 6.0f, 128) * scale;
        }
        public void setDuongdi(Vector2 ddau, Vector2 ddich, int target)
        {
            gocquay = 0;
            isEnable = true;
            diemdau = ddau;
            diemdich = ddich;
            this.target = target;
            // tính đường đi
            #region
            if (Math.Abs(diemdich.X - diemdau.X) > Math.Abs(diemdich.Y - diemdau.Y))
            {
                duongdi = new Vector2(1, (diemdich.Y - diemdau.Y) / (diemdich.X - diemdau.X));
                if (diemdich.X < diemdau.X) duongdi *= -1;
            }
            else
            {
                duongdi = new Vector2((diemdich.X - diemdau.X) / (diemdich.Y - diemdau.Y), 1);
                if (diemdich.Y < diemdau.Y) duongdi *= -1;
            }
            #endregion

            //tính góc quay và độ lệch phù hợp 
            #region
            if (duongdi.X == 0)
            {
                if (duongdi.Y > 0)
                {
                    gocquay = MathHelper.Pi;
                    dolech = new Vector2(size.X, 2 * size.Y - 10);
                }
                else
                {
                    gocquay = 0;
                    dolech = new Vector2(0, 0);
                }
            }
            else
            {
                if (duongdi.X > 0)
                {
                    gocquay = (float)Math.Atan(duongdi.Y / duongdi.X) + MathHelper.Pi / 2;
                    if (duongdi.Y <= 0)
                    {
                        dolech = new Vector2(60 * scale, -40 * scale);
                    }
                    else
                    {
                        dolech = new Vector2(140 * scale, -30 * scale);
                    }
                }
                else // hướng trái
                {
                    gocquay = (float)Math.Atan(duongdi.Y / duongdi.X) - MathHelper.Pi / 2;
                    if (duongdi.Y <= 0)
                    {
                        dolech = new Vector2(-40 * scale, 60 * scale);
                    }
                    else
                    {
                        dolech = new Vector2(90, 120) * scale;
                    }
                }
            }
            #endregion

            position = diemdau;

        }
        public Dan(int loai, float tocdo, Vector2 diemdau, Vector2 diemdich, float satthuong)
        {
            isEnable = true;
            this.satthuong = satthuong;
            this.loai = loai;
            this.tocdo = tocdo;
            this.diemdau = diemdau;
            position = diemdau;
            this.diemdich = diemdich;
            duongdi = new Vector2(1, (diemdich.Y - diemdau.Y) / (diemdich.X - diemdau.X));
            scale = GamePage.scale;
            size = new Vector2(768 / 6.0f, 128) * scale;
        }
        public void Load(ContentManager x)
        {
            image = x.Load<Texture2D>("images/bullet/bullet");
            size = new Vector2(768 / 6.0f, 128) * scale;
        }
        public void Draw(SpriteBatch spr)
        {
            scale = GamePage.scale;
            spr.Draw(image, (position + dolech) * scale + GamePage.DoLech, new Rectangle(loai * 768 / 6, 0, 768 / 6, 128),
                                         Color.White, gocquay, Vector2.Zero, scale, SpriteEffects.None, 0);
        }
        public void Dichuyen(List<Enemy> enemy)
        {
            position += tocdo * duongdi;
            if ((target <= enemy.Count - 1) && enemy[target].getEnable())
            {
                if (enemy[target].isVaCham(position) || Vector2.Distance(diemdich, position) <= (tocdo / Math.Sqrt(2)))
                {
                    isEnable = false;
                    enemy[target].setTruMau(satthuong, hieuung);
                }
            }
            else
            {
                for (int i = 0; i < enemy.Count; i++)
                {
                    if (enemy[i].isVaCham(position))
                    {
                        isEnable = false;
                        enemy[i].setTruMau(satthuong, hieuung);
                        break;
                    }
                }
                if (Vector2.Distance(diemdich, position) <= (tocdo / Math.Sqrt(2))) isEnable = false;
            }
        }
        public void setType(int type)
        {
            loai = type;
        }
        public void setSatthuong(float a, string hu)
        {
            satthuong = a;
            hieuung = hu;
        }
    }
}
